package com.my.hp_hr.contorller.liuhao_controller;

import com.my.hp_hr.entity.liuhao_entity.Employee;
import com.my.hp_hr.entity.liuhao_entity.Leave;
import com.my.hp_hr.service.liuhao_service.LeaveService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/oneself_leaveList")
public class EmployeeLeaveListServlet extends HttpServlet {
    public LeaveService leaveService=new LeaveService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Employee esl= (Employee) req.getSession().getAttribute("employee_isLogin");
            String pageNo = req.getParameter("pageNo");
            if (pageNo==null||"".equals(pageNo.trim())){
                pageNo="1";
            }
            int current=Integer.valueOf(pageNo);
            int pagesize=5;
            int total=leaveService.toGetTotal(esl.getEmployee_number());
            int pages=(int)(Math.ceil(total/5.0));
            List<Leave> leaveList = leaveService.pageLeaveList(current, pagesize,esl.getEmployee_number());
            for (Leave leave:leaveList){
                leave.setEmployee(esl);
            }
            req.setAttribute("leaveList",leaveList);
            Page page = new Page(current, total, pages);
            req.setAttribute("page",page);
            /*req.setAttribute("current",current);
            req.setAttribute("total",total);
            req.setAttribute("pages",pages);*/
            req.getRequestDispatcher("oneself_leave.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
